#pragma once

#include <string>

#include "topdbs_common.hpp"
#include "topdbs_field.hpp"
#include "topdbs_base_connection.hpp"
#include "topdbs_base_session.hpp"
#include "topdbs_connection.hpp"

namespace topdbs
{
	class TOPDBS_API SQLSession
	{	
	private:
		SQLConnection*   m_pConnection;
		topdbs_base_session* m_pSession;
	public:	
		SQLSession();
		SQLSession(SQLConnection* pConnection);
		~SQLSession();

		static int OpenSession(SQLSession& dbsCmd);

		bool status();
		void setConnection(SQLConnection* pConnection);
		SQLConnection* getConnection() const;
		void setCommandText(const string& sSQL,eDBSCommandType_t commandType = DBS_CmdUnknown);
		string getCommandText();
		eDBSCommandType_t CommandType() const;
		eDBSClient_t DbsClient() const;

		int  begin();
		int  commit();
		int  rollback();
		bool isTransaction();	

		//! Returns row data.
		stringmap_row_t& getRow();
		//! Tests whether a result set exists after the command execution.
		bool isResultSet();
		//! Returns number of rows affected by last DML operation.
		long RowsAffected();     
		inline long GetSqlRows() { return RowsAffected(); };
		//! Returns isExecuted
		bool IsExecuted();	
		
		//! Returns the current command executes errors information.
		std::string getErrors();
		std::string getSqlcode();
		std::string getSqlerr();	
		
		//! Returns fetch field value.
		int GetField(const std::string& iFiled,std::string& oValue);
		int GetField(const std::string& iFiled,char* oValue);
		int GetField(const std::string& iFiled,int&  oValue);
		int GetField(const std::string& iFiled,long& oValue);

		//! reset the current command cache data for the next operation
		void Reset();
		//! Validates and prepares the current command for execution.
		int  Prepare();
		//! Executes the current command.
		int  Execute() ; 
	#ifdef WIN32	
		int  Execute(const char* sSQL,...);
		int  Execute(string sSQL,...);
	#else
		int  Execute(const string& sSQL,...);	
	#endif	
		//! Executes the current command. only fetch one row data.
		int  ExecuteFetch(const string& sSQL);
		//! Fetches next row from a result set.
		bool FetchNext();
		//! Fetches next row from a result cursor.
		bool FetchCursorNext(uint16_t nFetchSize = TOPDBS_DEFAULT_FETCH_SIZE);
		//! Returns the current command params num
		int  NumsParams();
		//! Binding the command parameter specified by its position
		int  BindParams(const uint16_t paramno,const DBSValue& value);
		int  BindParams(const uint16_t paramno,const string& value);
		int  BindParams(const uint16_t paramno,const int     value);
		int  BindParams(const uint16_t paramno,const long    value);
		int  BindParams(const uint16_t paramno,const float   value);
		int  BindParams(const uint16_t paramno,const double  value);
	};
}
